package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOCustomerLogs extends DataAccesObject implements IfDAO{

	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM customerLogs " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOCustomerLogs clog = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM customerLogs " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				clog = new DTOCustomerLogs(rs.getInt("id"), rs.getInt("idCustomer"), 
						rs.getTime("date"), rs.getString("log"), rs.getString("ip"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return clog;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOCustomerLogs clog = (DTOCustomerLogs) bean;
		Connection con = getConexion();
		String query = "INSERT INTO customerLogs VALUES( DEFAULT"+
						", '"+clog.getIdCustomer() + "'" +
						", '"+clog.getDateToString() + "'" +
						", '"+clog.getLog() + "'" +
						", '"+clog.getIp() + "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> clogs = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM customerLogs " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOCustomerLogs clog = new DTOCustomerLogs(rs.getInt("id"), rs.getInt("idCustomer"), 
						rs.getTime("date"), rs.getString("log"), rs.getString("ip"));
				clogs.add(clog);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return clogs;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOCustomerLogs clog = (DTOCustomerLogs) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE customerLogs SET" +
						" idCustomer='" + clog.getIdCustomer() + "'" +
						" , date='" + clog.getDateToString() + "'" +
						" , log='" + clog.getLog() + "'" +
						" , ip='" + clog.getIp() + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}
}
